package com.example.myapplication.view;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.myapplication.R;
import com.example.myapplication.databinding.FragmentCheckChartBinding;
import com.example.myapplication.widgets.calendar.statistics.StatisticsBean;
import com.example.myapplication.widgets.chart.HistogramBean;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@FragmentName("柱状图")
public class CheckChartFragment extends Fragment {

    private FragmentCheckChartBinding mBinding;
    private int mDataType;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = FragmentCheckChartBinding.inflate(inflater);
        return mBinding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mBinding.progress.setOnClickListener(v -> {
            int progress = mBinding.progress.getProgress();
            progress += 10;
            mBinding.progress.setProgress(progress);
        });

        initHistogramViewWeek();
        initStatisticsView();
        initWaveView();

        mBinding.changeDataType.setOnClickListener(v -> {
            mBinding.histogramView.clear();
            if (mDataType == 0) {
                mDataType = 1;
                initHistogramViewMonth();
                mBinding.changeDataType.setText("切换周月年/月");
            } else if (mDataType == 1) {
                mDataType = 2;
                initHistogramViewYear();
                mBinding.changeDataType.setText("切换周月年/年");
            } else {
                mDataType = 0;
                initHistogramViewWeek();
                mBinding.changeDataType.setText("切换周月年/周");
            }
        });
    }

    private void initHistogramViewWeek() {
        List<String> axisXTxt = new ArrayList<>();
        axisXTxt.add("09-29\n周一");
        axisXTxt.add("09-30\n周二");
        axisXTxt.add("10-01\n周三");
        axisXTxt.add("10-02\n周四");
        axisXTxt.add("10-03\n周五");
        axisXTxt.add("10-04\n周六");
        axisXTxt.add("10-05\n周日");
        mBinding.histogramView.setAxisInfo(axisXTxt);
        mBinding.histogramView.setHistogramWidth(getResources().getDimensionPixelSize(R.dimen.dp_10));

        List<HistogramBean> data = new ArrayList<>();
        HistogramBean bean;
        Random random = new Random();
        int bound = random.nextInt(10000);
        for (int i = 1; i <= 7; i++) {
            bean = new HistogramBean();
            bean.axisX = axisXTxt.get(i - 1);
            bean.axisY = random.nextInt(bound);
            data.add(bean);
        }
        mBinding.histogramView.setData(data, 5);

        mBinding.changeValue.setOnClickListener(v -> {
            List<HistogramBean> changeData = new ArrayList<>();
            HistogramBean changeBean;
            int newBound = random.nextInt(10000);
            for (int i = 1; i <= 7; i++) {
                changeBean = new HistogramBean();
                changeBean.axisX = axisXTxt.get(i - 1);
                changeBean.axisY = random.nextInt(newBound);
                changeData.add(changeBean);
            }
            mBinding.histogramView.setData(changeData, 5);
        });
    }

    private void initHistogramViewMonth() {
        mBinding.changeValue.setOnClickListener(v -> {
            List<HistogramBean> data = new ArrayList<>();
            HistogramBean bean;
            Random random = new Random();
            int bound = random.nextInt(10000);
            for (int i = 1; i < 32; i++) {
                bean = new HistogramBean();
                bean.axisX = String.valueOf(i);
                bean.axisY = random.nextInt(bound);
                data.add(bean);
            }
            mBinding.histogramView.setData(data, 5);
        });

        List<String> axisXTxt = new ArrayList<>();
        for (int i = 1; i < 32; i++) {
            axisXTxt.add(String.valueOf(i));
        }
        mBinding.histogramView.setAxisInfo(axisXTxt);
        mBinding.histogramView.setJustDrawCenterAxisX(true);
        mBinding.histogramView.setHistogramWidth(getResources().getDimensionPixelSize(R.dimen.dp_4));

        List<HistogramBean> data = new ArrayList<>();
        HistogramBean bean;
        Random random = new Random();
        int bound = random.nextInt(10000);
        for (int i = 1; i < 32; i++) {
            bean = new HistogramBean();
            bean.axisX = String.valueOf(i);
            bean.axisY = random.nextInt(bound);
            data.add(bean);
        }
        mBinding.histogramView.setData(data, 5);
    }

    private void initHistogramViewYear() {
        List<String> axisXTxt = new ArrayList<>();
        for (int i = 1; i < 13; i++) {
            axisXTxt.add(String.valueOf(i));
        }
        List<Float> axisYTxt = new ArrayList<>();
        axisYTxt.add(25f);
        axisYTxt.add(50f);
        axisYTxt.add(75f);
        axisYTxt.add(100f);
        mBinding.histogramView.setAxisInfo(axisXTxt, axisYTxt);
        mBinding.histogramView.setHistogramWidth(getResources().getDimensionPixelSize(R.dimen.dp_10));
        List<HistogramBean> data = new ArrayList<>();
        HistogramBean bean;
        Random random = new Random();
        for (int i = 1; i <= 12; i++) {
            bean = new HistogramBean();
            bean.axisX = String.valueOf(i);
            bean.axisY = i;
//            bean.axisY = random.nextInt(10);
            data.add(bean);
        }

        mBinding.histogramView.setData(data);
    }

    private void initWaveView() {
        mBinding.changeWaveValue.setOnClickListener(v -> {
            float[] position = new float[7];
            Random r = new Random();
            int bound = r.nextInt(10000);
            for (int i = 0; i < position.length; i++) {
                position[i] = r.nextInt(bound);
            }
            mBinding.waveView.cancelAnimator();
            mBinding.waveView.clear();
            mBinding.waveView.addWave(Color.parseColor("#29c62f"), position);
            mBinding.waveView.addWaveCoverRegion(Color.parseColor("#7f29C62F"), Color.parseColor("#0029C62F"),
                    position);
//            mBinding.waveView.showAnimator();
        });

        mBinding.waveView.setVisibility(View.VISIBLE);
        // 初始化数据表格相关
        // 配置坐标系
        mBinding.waveView.setupCoordinator(7,
                /*这里是横坐标的值*/"周一", "周二", "周三", "周四", "周五", "周六", "周日");
        float[] position = new float[]{500f, 500f, 3220f, 1121f, 270f, 325f, 50f};
        // 添加曲线, 确保纵坐标的数值位数相等
        mBinding.waveView.addWave(Color.parseColor("#29c62f"), position);
        mBinding.waveView.addWaveCoverRegion(Color.parseColor("#7f29C62F"), Color.parseColor("#0029C62F"),
                position);
//        mBinding.waveView.showAnimator();
    }

    private void initStatisticsView() {
        List<StatisticsBean> statisticsBeanList = new ArrayList<>();
        StatisticsBean statisticsBean;
        statisticsBean = new StatisticsBean();
        statisticsBean.date = "2023/08/02";
        statisticsBean.status = 1;
        statisticsBeanList.add(statisticsBean);
        statisticsBean = new StatisticsBean();
        statisticsBean.date = "2023/08/03";
        statisticsBean.status = 1;
        statisticsBeanList.add(statisticsBean);
        statisticsBean = new StatisticsBean();
        statisticsBean.date = "2023/08/04";
        statisticsBean.status = 1;
        statisticsBeanList.add(statisticsBean);
        statisticsBean = new StatisticsBean();
        statisticsBean.date = "2023/08/05";
        statisticsBean.status = 1;
        statisticsBeanList.add(statisticsBean);
        statisticsBean = new StatisticsBean();
        statisticsBean.date = "2023/08/06";
        statisticsBean.status = 1;
        statisticsBeanList.add(statisticsBean);
        statisticsBean = new StatisticsBean();
        statisticsBean.date = "2023/08/07";
        statisticsBean.status = 1;
        statisticsBeanList.add(statisticsBean);
        statisticsBean = new StatisticsBean();
        statisticsBean.date = "2023/08/10";
        statisticsBean.status = 1;
        statisticsBeanList.add(statisticsBean);
        statisticsBean = new StatisticsBean();
        statisticsBean.date = "2023/08/11";
        statisticsBean.status = -1;
        statisticsBeanList.add(statisticsBean);
        statisticsBean = new StatisticsBean();
        statisticsBean.date = "2023/08/20";
        statisticsBean.status = 0;
        statisticsBeanList.add(statisticsBean);
        statisticsBean = new StatisticsBean();
        statisticsBean.date = "2023/08/22";
        statisticsBean.status = 1;
        statisticsBeanList.add(statisticsBean);

        mBinding.statisticsView.setDateInfo(2023, 8);
        mBinding.statisticsView.setData(statisticsBeanList);

        List<StatisticsBean> weekStatisticsBeanList = new ArrayList<>();
        statisticsBean = new StatisticsBean();
        statisticsBean.date = "2023/08/07";
        statisticsBean.status = 1;
        weekStatisticsBeanList.add(statisticsBean);
        statisticsBean = new StatisticsBean();
        statisticsBean.date = "2023/08/08";
        statisticsBean.status = -1;
        weekStatisticsBeanList.add(statisticsBean);
        statisticsBean = new StatisticsBean();
        statisticsBean.date = "2023/08/09";
        statisticsBean.status = 0;
        weekStatisticsBeanList.add(statisticsBean);
        statisticsBean = new StatisticsBean();
        statisticsBean.date = "2023/08/12";
        statisticsBean.status = 1;
        weekStatisticsBeanList.add(statisticsBean);

        mBinding.statisticsView2.setDateInfo(2023, 8, 7);
        mBinding.statisticsView2.setData(weekStatisticsBeanList);
    }
}
